package com.ssmeasy.jdgl.controller;

import com.ssmeasy.jdgl.pojo.User;
import com.ssmeasy.jdgl.service.UserService;
import com.ssmeasy.jdgl.utils.CookieUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {


    @Autowired
    private UserService userService;

    /**
     * @api {post} /user/login 登陆
     * @apiName userLogin
     * @apiGroup User
     * @apiVersion 0.1.0
     *
     * @apiDescription  登陆
     *
     * @apiParam {String} username  用户名
     * @apiParam {String} password  密码
     *
     *
     * @apiSuccess {boolean} res 是否成功
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "res":true
     * }
     *
     * @apiErrorExample {json} Error-Response:
     *  HTTP/1.1 200 OK
     *  {
     *  	"res":false
     *  }
     */
    @RequestMapping("/login")
    @ResponseBody
    public Map<String,Boolean> userLogin(String username, String password, HttpServletResponse response){
        //返回值Map
        Map<String,Boolean> result = new HashMap<>();
        //登陆服务，失败返回null
        User user = userService.login(username,password);

        if(user!=null){
            //调用Cookie工具类，设置username
            CookieUtils.setCookie(response,"username",user.getUsername());
            result.put("res",true);
        }
        else
            result.put("res",false);

        return result;
    }


    /**
     * @api {post} /user/logout/secure 注销
     * @apiName userLoginout
     * @apiGroup User
     * @apiVersion 0.1.0
     *
     * @apiDescription 注销
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "res":true
     * }
     *
     * @apiErrorExample {json} Error-Response:
     *  HTTP/1.1 200 OK
     *  {
     *  	"res":false
     *  }
     */
    @RequestMapping("/logout/secure")
    @ResponseBody
    public Map<String,Boolean> userLoginout(HttpServletRequest request,HttpServletResponse response){
        //返回值Map
        Map<String,Boolean> result = new HashMap<>();
        //使用Cookie工具类移除 username，退出登陆
        Boolean res = CookieUtils.removeCookie("username",request,response);

        result.put("res",res);
        return result;
    }

    /**
     * @api {post} /user/getUsername/secure 获取当前用户名
     * @apiName getusername
     * @apiGroup User
     * @apiVersion 0.1.0
     *
     * @apiDescription 获取当前用户名
     *
     *
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "username":"yg0001"
     * }
     *
     * @apiErrorExample {json} Error-Response:
     *  HTTP/1.1 200 OK
     *  {
     *  	"username":null
     *  }
     */
    @RequestMapping("/getUsername/secure")
    @ResponseBody
    public Map<String,String> get(HttpServletRequest request){
        //返回值Map  <username，对应的用户名>
        Map<String,String> result = new HashMap<>();

        //在Cookie中获取
        String username = CookieUtils.getCookie(request,"username");
        if(!username.isEmpty()){
            result.put("username",username);
        }
        else
            result.put("username",null);

        return result;
    }


    /**
     * @api {post} /user/updatePassword/secure 修改密码
     * @apiName updatePassword
     * @apiGroup User
     * @apiVersion 0.1.0
     *
     * @apiDescription 修改密码
     *
     * @apiParam {String} oldPassword  用户名
     * @apiParam {String} newPassword  密码
     *
     *
     * @apiSuccess {boolean} res 是否成功
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "res":true
     * }
     *
     * @apiErrorExample {json} Error-Response:
     *  HTTP/1.1 200 OK
     *  {
     *  	"res":false
     *  }
     */
    @RequestMapping("/updatePassword/secure")
    @ResponseBody
    public Map<String, Boolean> update(String oldPassword, String newPassword, HttpServletRequest request){
        Map<String, Boolean> result = new HashMap<>();
        String username = CookieUtils.getCookie(request, "username");


        try {
            userService.updatePassword(username, oldPassword, newPassword);
            result.put("res", true);
        }catch (Exception e){
            result.put("res", false);
        }



        return result;
    }
}
